import { User, UserFormData, UserQueryParams, UserListResponse, UserRole, UserStatus } from '../types/user';

// 模拟用户数据
const mockUsers: User[] = [
  {
    id: 1,
    username: 'admin',
    email: 'admin@example.com',
    phone: '13800138000',
    role: UserRole.SUPER_ADMIN,
    status: UserStatus.ACTIVE,
    createdAt: '2024-01-01T00:00:00Z',
    lastLoginAt: '2024-03-15T10:30:00Z',
  },
  {
    id: 2,
    username: 'order_admin',
    email: 'order_admin@example.com',
    phone: '13800138001',
    role: UserRole.ORDER_ADMIN,
    status: UserStatus.ACTIVE,
    createdAt: '2024-01-02T00:00:00Z',
    lastLoginAt: '2024-03-14T15:20:00Z',
  },
  {
    id: 3,
    username: 'category_admin',
    email: 'category_admin@example.com',
    phone: '13800138002',
    role: UserRole.CATEGORY_ADMIN,
    status: UserStatus.ACTIVE,
    createdAt: '2024-01-03T00:00:00Z',
    lastLoginAt: '2024-03-13T09:15:00Z',
  },
  {
    id: 4,
    username: 'product_admin',
    email: 'product_admin@example.com',
    phone: '13800138003',
    role: UserRole.PRODUCT_ADMIN,
    status: UserStatus.ACTIVE,
    createdAt: '2024-01-04T00:00:00Z',
    lastLoginAt: '2024-03-12T14:30:00Z',
  },
];

// 获取用户列表
export const getUserList = async (params: UserQueryParams): Promise<UserListResponse> => {
  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 500));

  let filteredUsers = [...mockUsers];

  // 关键词搜索
  if (params.keyword) {
    const keyword = params.keyword.toLowerCase();
    filteredUsers = filteredUsers.filter(
      user =>
        user.username.toLowerCase().includes(keyword) ||
        user.email.toLowerCase().includes(keyword) ||
        user.phone.includes(keyword)
    );
  }

  // 角色筛选
  if (params.role) {
    filteredUsers = filteredUsers.filter(user => user.role === params.role);
  }

  // 状态筛选
  if (params.status) {
    filteredUsers = filteredUsers.filter(user => user.status === params.status);
  }

  // 分页
  const start = (params.page - 1) * params.pageSize;
  const end = start + params.pageSize;
  const items = filteredUsers.slice(start, end);

  return {
    total: filteredUsers.length,
    items,
  };
};

// 获取用户详情
export const getUserById = async (id: number): Promise<User | null> => {
  await new Promise(resolve => setTimeout(resolve, 300));
  return mockUsers.find(user => user.id === id) || null;
};

// 创建用户
export const createUser = async (userData: UserFormData): Promise<User> => {
  await new Promise(resolve => setTimeout(resolve, 500));

  const newUser: User = {
    id: Math.max(...mockUsers.map(u => u.id)) + 1,
    ...userData,
    createdAt: new Date().toISOString(),
    lastLoginAt: new Date().toISOString(),
  };

  mockUsers.push(newUser);
  return newUser;
};

// 更新用户
export const updateUser = async (id: number, userData: Partial<UserFormData>): Promise<User | null> => {
  await new Promise(resolve => setTimeout(resolve, 500));

  const index = mockUsers.findIndex(user => user.id === id);
  if (index === -1) return null;

  const updatedUser = {
    ...mockUsers[index],
    ...userData,
  };

  mockUsers[index] = updatedUser;
  return updatedUser;
};

// 删除用户
export const deleteUser = async (id: number): Promise<boolean> => {
  await new Promise(resolve => setTimeout(resolve, 500));

  const index = mockUsers.findIndex(user => user.id === id);
  if (index === -1) return false;

  mockUsers.splice(index, 1);
  return true;
};

// 更新用户状态
export const updateUserStatus = async (id: number, status: UserStatus): Promise<User | null> => {
  return updateUser(id, { status });
};

// 更新用户角色
export const updateUserRole = async (id: number, role: UserRole): Promise<User | null> => {
  return updateUser(id, { role });
}; 